from odoo import fields, models, api


class PeDeviceAssets(models.Model):
    _name = 'pe.device.assets'
    _inherit = ["mail.thread", "mail.activity.mixin"]
    _description = '设备'
    _rec_name = 'pe_internal_name'
    _sql_constraints = [
        ('pe_internal_name_unique', 'unique (pe_internal_code)', "内部编码需要唯一！ß"),
    ]

    sale_name = fields.Char(string='销售名称', translate=True,tracking=True)
    product_sale_model = fields.Char(string='销售型号',tracking=True)
    purchase_name = fields.Char(string='采购名称',tracking=True)
    product_purchase_model = fields.Char(string='采购型号',tracking=True)
    pe_internal_code = fields.Char(string='内部编码',tracking=True)
    pe_internal_name = fields.Char(string='内部名称',tracking=True)
    pe_device_category_id = fields.Many2one(string='类型', comodel_name='pe.device.category',tracking=True)
    pe_device_brand_id = fields.Many2one(string='品牌', comodel_name='pe.device.brand',tracking=True)
    pe_device_tag_ids = fields.Many2many(string='标签', comodel_name='pe.device.tag', column1='pe_device_assets_id', column2='pe_device_tag_id',
                                         relation='pe_device_assets_device_tag_rel',tracking=True)
    pe_testing_for_tech = fields.Boolean(string='技术用测试机',tracking=True)
    pe_testing_for_marketing = fields.Boolean(string='运营用测试机',tracking=True)

    pe_device_image = fields.Many2many(string='图片', comodel_name='ir.attachment',tracking=True)
    feature = fields.Text(string='特点',tracking=True)
    pe_device_accessory_line_ids = fields.One2many(string='配件明细', comodel_name='pe.device.accessory.line', inverse_name='pe_device_assets_id',tracking=True)
    pe_property_line_ids = fields.One2many(string='属性明细', comodel_name='pe.property.line', inverse_name='pe_device_assets_id',tracking=True)
    purchase_time = fields.Date(string='购入时间',tracking=True)
    amount = fields.Integer(string='数量',tracking=True)
    remark = fields.Char(string='备注',tracking=True)

    """
    Native
    """


    def name_get(self):
        result = []
        for record in self:
            if self.env.context.get('show_name'):
                result.append((record.id, f"{record.pe_internal_name}[{record.pe_internal_code}]"))
            else:
                result.append((record.id, record.pe_internal_name))
        return result

    """
    Onchange
    """

    @api.onchange('pe_testing_for_tech')
    def onchange_on_pe_testing_for_tech(self):
        if self.pe_testing_for_tech:
            self.pe_testing_for_marketing=False

    @api.onchange('pe_testing_for_marketing')
    def onchange_on_pe_testing_for_marketing(self):
        if self.pe_testing_for_marketing:
            self.pe_testing_for_tech=False




class PeDeviceAccessoryLine(models.Model):
    _name = 'pe.device.accessory.line'
    _description = '配件明细'

    pe_device_assets_id = fields.Many2one(string='主表', comodel_name='pe.device.assets')
    pe_device_accessory_id = fields.Many2one(string='配件', comodel_name='pe.device.accessory')
    pe_device_accessory_category_id = fields.Many2one(string='配件类别', comodel_name='pe.device.accessory.category', related='pe_device_accessory_id.pe_device_accessory_category_id', store=True)
    description = fields.Char(string='说明')


class PePropertyLine(models.Model):
    _name = 'pe.property.line'
    _description = '属性明细'

    pe_device_assets_id = fields.Many2one(string='主表', comodel_name='pe.device.assets')
    pe_device_property_id = fields.Many2one(string='属性类别', comodel_name='pe.device.property.category')
    description = fields.Char(string='说明')
